package org.jash.sportnews2109.entity

import android.graphics.Color
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import androidx.databinding.BaseObservable
import androidx.databinding.Bindable
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.PrimaryKey
import com.alibaba.android.arouter.launcher.ARouter
import org.jash.sportnews2109.BR

@Entity
data class News(
    val content: String,
    val createTime: String,
    val flag: Int,
    @PrimaryKey
    val id: Int,
    val imgurl: String,
    val looks: Int,
    val ntid: Int,
    val suid: Int,
    val title: String,
    val comments: Int,
    val collected: Int
): BaseObservable() {
    @Bindable
    var collects: Int = 0
        set(value) {
            field = value
            notifyPropertyChanged(BR.collects)
        }

    fun showDetail() {
        ARouter.getInstance()
            .build("/news/detail")
            .withInt("id", id)
            .navigation()
    }
    @Ignore
    var key:String? = null
    @get:Ignore
    val highlightTitle:CharSequence
        get() = key?.let {
                val resultSequence = it.toRegex().findAll(title)
                SpannableString(title).apply {
                    resultSequence.forEach { result ->
                        val range = result.groups[0]?.range ?: 0..0
                        setSpan(ForegroundColorSpan(Color.BLUE), range.start, range.last + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                    }
                }
            } ?: title

}